from google.appengine.ext import webapp
from models.models import Article
from google.appengine.ext.webapp import template
import os
from views import _slugify
import logging

class ArticleHandler(webapp.RequestHandler):
    
    def get(self, id):
        _get_context = {}
        _get_context['news'] = Article.gql("WHERE id_title = '%s'" % id ).get()
        path = os.path.join(os.path.dirname(__file__), 'templates', 'article.html')
        
        
        self.response.out.write(template.render(path, _get_context))
        return 
    
    
    
class AddArticleHandler(webapp.RequestHandler):
    def get(self):
        _get_context = {}
        path = os.path.join(os.path.dirname(__file__), 'templates', 'add_article.html')
        self.response.out.write(template.render(path, _get_context))
        return 
    
    def post(self):
        title = self.request.POST.get('title')
        author = self.request.POST.get('author')
        content = self.request.POST.get('content')
        image = self.request.POST.get('image')
        sources = self.request.POST.get('sources')
        article = Article(id_title = _slugify(title), title = title, author = author, content = content, image = image, source = sources.split(' '))
        article.put()
        
        self.redirect('/')
        return
        